package com.micro.ai.files.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.files.entity.FileOperation;
import com.micro.ai.files.service.FileOperationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 文件操作日志控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/file-operations")
@Tag(name = "文件操作日志管理", description = "文件操作日志的记录和查询功能")
public class FileOperationController {

    @Autowired
    private FileOperationService fileOperationService;

    @PostMapping
    @PreAuthorize("hasAuthority('file_operation:create')")
    @Operation(summary = "记录操作日志", description = "记录文件操作日志")
    public ApiResponse<FileOperation> record(@Valid @RequestBody FileOperation operation) {
        FileOperation result = fileOperationService.record(operation);
        return ApiResponse.success("记录成功", result);
    }

    @GetMapping("/page")
    @PreAuthorize("hasAuthority('file_operation:view')")
    @Operation(summary = "分页查询操作日志", description = "支持多条件查询操作日志列表")
    public ApiResponse<Page<FileOperation>> pageQuery(
            @Parameter(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "20") @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @Parameter(description = "租户ID") @RequestParam(value = "tenantId", required = false) String tenantId,
            @Parameter(description = "文件ID") @RequestParam(value = "fileId", required = false) String fileId,
            @Parameter(description = "操作人ID") @RequestParam(value = "operatorId", required = false) String operatorId,
            @Parameter(description = "操作类型") @RequestParam(value = "operationType", required = false) String operationType) {
        Page<FileOperation> page = fileOperationService.pageQuery(pageNum, pageSize, tenantId, fileId, operatorId, operationType);
        return ApiResponse.success(page);
    }

    @GetMapping("/by-file/{fileId}")
    @PreAuthorize("hasAuthority('file_operation:view')")
    @Operation(summary = "查询文件的操作日志", description = "根据文件ID查询操作日志")
    public ApiResponse<List<FileOperation>> listByFileId(
            @Parameter(description = "文件ID", required = true) @PathVariable String fileId) {
        List<FileOperation> list = fileOperationService.listByFileId(fileId);
        return ApiResponse.success(list);
    }

    @GetMapping("/by-operator")
    @PreAuthorize("hasAuthority('file_operation:view')")
    @Operation(summary = "查询用户的操作日志", description = "根据用户ID查询操作日志")
    public ApiResponse<List<FileOperation>> listByOperator(
            @Parameter(description = "租户ID", required = true) @RequestParam("tenantId") String tenantId,
            @Parameter(description = "操作人ID", required = true) @RequestParam("operatorId") String operatorId) {
        List<FileOperation> list = fileOperationService.listByOperator(tenantId, operatorId);
        return ApiResponse.success(list);
    }
}

